﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EzDesk.Core.Interface;
using EzDesk.Core.ServiceImpl.Repositories;
using EzDesk.Core.ServiceImpl.Services;
using Moq;
using NUnit.Framework;
using EzDesk.Core.Model;
using CoreService = EzDesk.Core.ServiceImpl.Services.CoreService;

namespace EzDesk.Core.UnitTest
{
    ///////////////
    /////
    // 对Permission相关CoreService的测试方法
    // 由于没有对PermissionRepository做相应的Mock，因此当前测试需要依赖数据库中的示例数据:
    // 
    //  可用的组名称：
    // attendance_hr     /* 卢春叶 */
    // core_boss   /* 贺旭华  */
    // core_employee    所有人
    //
    // 可用的权限名称
    //  ATTENDANCE_HR_MANAGEMENT  人事管理全新啊
    //  ATTENDANCE_AUDIT_LEAVE   
    //


    /// <summary>
    /// 
    /// </summary>
    [TestFixture]
    public class CoreAppServicePermissionFixture
    {
        private ICoreService _coreService;

        private Group attendance_hr_group = null;
        private Group core_boss_group = null;
        private Group core_employee_group = null;

        public CoreAppServicePermissionFixture() { }

        [SetUp]
        public void Setup()
        {
            var db = new EzDeskDataContext();
            _coreService = new CoreService(
                new UserRepository(db), new DeptRepository(db), new AppRepository(db), new PositionRepository(db),
                new GroupRepository(db), new FeedRepository(db), new PermissionRepository(db), new OrganizationRepository(db)
                );

            attendance_hr_group = _coreService.GetGroupByName("attendance_hr");
            core_boss_group = _coreService.GetGroupByName("core_boss");
            core_employee_group = _coreService.GetGroupByName("core_employee");

        }


        [Test]
        public void Test_各个Group的权限()
        {
            var coreAppService = new CoreAppService(_coreService);
            Assert.AreEqual(true, coreAppService.GroupHasPermission("ATTENDANCE_HR_MANAGEMENT", attendance_hr_group.Id));
            Assert.AreEqual(true, coreAppService.GroupHasPermission("ATTENDANCE_HR_MANAGEMENT", core_boss_group.Id));
            Assert.AreEqual(false, coreAppService.GroupHasPermission("ATTENDANCE_HR_MANAGEMENT", core_employee_group.Id));

            Assert.AreEqual(false, coreAppService.GroupHasPermission("ATTENDANCE_AUDIT_LEAVE", attendance_hr_group.Id));
            Assert.AreEqual(true, coreAppService.GroupHasPermission("ATTENDANCE_AUDIT_LEAVE", core_boss_group.Id));
            Assert.AreEqual(false, coreAppService.GroupHasPermission("ATTENDANCE_AUDIT_LEAVE", core_employee_group.Id));
        }

        [Test]
        public void Test_贺旭华的用户权限()
        {
            var coreAppService = new CoreAppService(_coreService);
            var user = coreAppService.GetUserProfileByUserName("hxh");
            Assert.AreEqual(true, coreAppService.UserHasPermission("ATTENDANCE_HR_MANAGEMENT", user.Id));
            Assert.AreEqual(true, coreAppService.UserHasPermission("ATTENDANCE_AUDIT_LEAVE", user.Id));
        }

        [Test]
        public void Test_卢春叶的用户权限()
        {
            var coreAppService = new CoreAppService(_coreService);
            var user = coreAppService.GetUserProfileByUserName("lcy");
            Assert.AreEqual(true, coreAppService.UserHasPermission("ATTENDANCE_HR_MANAGEMENT", user.Id));
            Assert.AreEqual(false, coreAppService.UserHasPermission("ATTENDANCE_AUDIT_LEAVE", user.Id));
        }

        [Test]
        public void Test_龚伟的用户权限()
        {
            var coreAppService = new CoreAppService(_coreService);
            var user = coreAppService.GetUserProfileByUserName("gw");
            Assert.AreEqual(false, coreAppService.UserHasPermission("ATTENDANCE_HR_MANAGEMENT", user.Id));
            Assert.AreEqual(false, coreAppService.UserHasPermission("ATTENDANCE_AUDIT_LEAVE", user.Id));
        }
    }
}
